public class Solution {
    public int[] exchange(int[] nums) {
        if (nums.length == 0) {return nums;} // 一定要先写这句，注意特殊情况
        int i = 0, j = nums.length - 1;
        while(i < j) {
            if (nums[i] % 2 == 1) {i++;continue;}
            if (nums[j] % 2 == 0) {j--; continue;} // 第一次错在了把continue写为了break
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
        return nums;
    }

    /*错误的做法*/
    public int[] exchange2(int[] nums) {
        if (nums.length == 0) return nums;
        int i = 0, j = nums.length - 1;

        while (i < j) {
            while (nums[i] % 2 == 1) i++; /*遇到双指针一定不要写这种写法，要写上面的写法，这种会越界*/
            while (nums[j] % 2 == 0) j--;
            if (i < j) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        return nums;
    }
}

